package com.example.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.operation.domin.Dispatch;
import com.example.operation.domin.DispatchCity;
import com.example.operation.domin.DispatchCondition;
import com.example.operation.domin.DispatchExpress;
import com.example.operation.dto.DispatchCityDto;
import com.example.operation.dto.DispatchConditionDto;
import com.example.operation.dto.DispatchDto;
import com.example.operation.dto.DispatchExpressDto;
import com.example.operation.mapper.DispatchCityMapper;
import com.example.operation.mapper.DispatchConditionMapper;
import com.example.operation.mapper.DispatchExpressMapper;
import com.example.operation.mapper.DispatchMapper;
import com.example.operation.service.DispatchService;
import com.example.operation.vo.DispatchCityVo;
import com.example.operation.vo.DispatchExpressVo;
import com.example.operation.vo.DispatchVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: DispatchServiceImpl
 * @Description:
 * @Author
 * @Date 2021/11/24
 * @Version 1.0
 */

@Service
public class DispatchServiceImpl extends ServiceImpl<DispatchMapper, Dispatch> implements DispatchService {


    @Autowired
    private DispatchMapper dispatchMapper;

    @Autowired
    private DispatchCityMapper dispatchCityMapper;

    @Autowired
    private DispatchExpressMapper dispatchExpressMapper;

    @Autowired
    private DispatchConditionMapper dispatchConditionMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insert(DispatchDto dispatchDto) {
        Dispatch dispatch = new Dispatch();
        LambdaQueryWrapper<Dispatch> eq = new QueryWrapper<Dispatch>()
                .lambda().eq(Dispatch::getName,dispatchDto.getName());
        Integer count = dispatchMapper.selectCount(eq);
        if(count > 0){
            throw new RuntimeException("模板名称不能重复");
        }
        BeanUtils.copyProperties(dispatchDto,dispatch);
        this.save(dispatch);

        DispatchCityDto dispatchCityDto =dispatchDto.getDispatchCityDto();
        if(dispatchCityDto != null){
            Integer dispatchId = this.getOne(eq).getId();
            DispatchCity dispatchCity = new DispatchCity();
            BeanUtils.copyProperties(dispatchCityDto,dispatchCity);
            dispatchCity.setAddress(dispatchCityDto.getAddress().toString());
            dispatchCity.setDispatchId(dispatchId);
            dispatchCityMapper.insert(dispatchCity);
        }

        DispatchExpressDto dispatchExpressDto = dispatchDto.getDispatchExpressDto();
        if(dispatchExpressDto != null){
            Integer dispatchId = this.getOne(eq).getId();
            DispatchExpress dispatchExpress = new DispatchExpress();
            BeanUtils.copyProperties(dispatchExpressDto,dispatchExpress);
            dispatchExpress.setAddress(dispatchExpressDto.getAddress().toString());
            dispatchExpress.setDispatchId(dispatchId);
            dispatchExpressMapper.insert(dispatchExpress);
        }

        DispatchConditionDto dispatchConditionDto = dispatchDto.getDispatchConditionDto();
        if(dispatchConditionDto != null){
            Integer dispatchId = this.getOne(eq).getId();
            DispatchCondition dispatchCondition = new DispatchCondition();
            BeanUtils.copyProperties(dispatchConditionDto,dispatchCondition);
            dispatchCondition.setAddress(dispatchConditionDto.getAddress().toString());
            dispatchCondition.setDispatchId(dispatchId);
            dispatchConditionMapper.insert(dispatchCondition);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(DispatchDto dispatchDto) {
        Dispatch dispatch = new Dispatch();
        LambdaUpdateWrapper<Dispatch> eq = new UpdateWrapper<Dispatch>()
                .lambda().eq(Dispatch::getName,dispatchDto.getName());
        Integer count = dispatchMapper.selectCount(eq);
        if(count == 0){
            throw new RuntimeException("模板名称不存在，请确认您输入的模板名称是否正确");
        }
        BeanUtils.copyProperties(dispatchDto,dispatch);
        this.updateById(dispatch);

        //删除已存在的同城配送信息
        LambdaQueryWrapper<DispatchCity> eq1 = new QueryWrapper<DispatchCity>()
                .lambda().eq(DispatchCity::getDispatchId,dispatchDto.getId());
        Integer count1 = dispatchCityMapper.selectCount(eq1);
        if(count1 > 0){
            dispatchCityMapper.delete(eq1);
        }

        //添加同城配送信息
        DispatchCityDto dispatchCityDto =dispatchDto.getDispatchCityDto();
        if(dispatchCityDto != null){
            Integer dispatchId = this.getOne(eq).getId();
            DispatchCity dispatchCity = new DispatchCity();
            BeanUtils.copyProperties(dispatchCityDto,dispatchCity);
            dispatchCity.setAddress(dispatchCityDto.getAddress().toString());
            dispatchCity.setDispatchId(dispatchId);
            dispatchCityMapper.insert(dispatchCity);
        }

        //删除已存在的快递物流信息
        LambdaQueryWrapper<DispatchExpress> eq2 = new QueryWrapper<DispatchExpress>()
                .lambda().eq(DispatchExpress::getDispatchId,dispatchDto.getId());
        Integer count2 = dispatchExpressMapper.selectCount(eq2);
        if(count2 > 0){
            dispatchExpressMapper.delete(eq2);
        }
        //添加同城配送信息
        DispatchExpressDto dispatchExpressDto = dispatchDto.getDispatchExpressDto();
        if(dispatchExpressDto != null){
            Integer dispatchId = this.getOne(eq).getId();
            DispatchExpress dispatchExpress = new DispatchExpress();
            BeanUtils.copyProperties(dispatchExpressDto,dispatchExpress);
            dispatchExpress.setAddress(dispatchExpressDto.getAddress().toString());
            dispatchExpress.setDispatchId(dispatchId);
            dispatchExpressMapper.insert(dispatchExpress);
        }
        //删除已存在的指定条件包邮信息
        LambdaQueryWrapper<DispatchCondition> eq3 = new QueryWrapper<DispatchCondition>()
                .lambda().eq(DispatchCondition::getDispatchId,dispatchDto.getId());
        Integer count3 = dispatchConditionMapper.selectCount(eq3);
        if(count3 > 0){
            dispatchConditionMapper.delete(eq3);
        }
        //添加指定条件包邮信息
        DispatchConditionDto dispatchConditionDto = dispatchDto.getDispatchConditionDto();
        if(dispatchConditionDto != null){
            Integer dispatchId = this.getOne(eq).getId();
            DispatchCondition dispatchCondition = new DispatchCondition();
            BeanUtils.copyProperties(dispatchConditionDto,dispatchCondition);
            dispatchCondition.setAddress(dispatchConditionDto.getAddress().toString());
            dispatchCondition.setDispatchId(dispatchId);
            dispatchConditionMapper.insert(dispatchCondition);
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer delectById(Integer id) {
        //删除已存在的同城配送信息
        LambdaQueryWrapper<DispatchCity> eq1 = new QueryWrapper<DispatchCity>()
                .lambda().eq(DispatchCity::getDispatchId,id);
        Integer count1 = dispatchCityMapper.selectCount(eq1);
        if(count1 > 0){
            dispatchCityMapper.delete(eq1);
        }
        //删除已存在的快递物流信息
        LambdaQueryWrapper<DispatchExpress> eq2 = new QueryWrapper<DispatchExpress>()
                .lambda().eq(DispatchExpress::getDispatchId,id);
        Integer count2 = dispatchExpressMapper.selectCount(eq2);
        if(count2 > 0){
            dispatchExpressMapper.delete(eq2);
        }
        //删除已存在的指定条件包邮信息
        LambdaQueryWrapper<DispatchCondition> eq3 = new QueryWrapper<DispatchCondition>()
                .lambda().eq(DispatchCondition::getDispatchId,id);
        Integer count3 = dispatchConditionMapper.selectCount(eq3);
        if(count3 > 0){
            dispatchConditionMapper.delete(eq3);
        }
        return dispatchMapper.deleteById(id);
    }

    @Override
    public Page<DispatchVo> page(Integer pageCurrent, Integer pageSize, Integer pageNumber, String name) {
        //创建分页对象
        Page<DispatchVo> page = new Page<>(pageCurrent,pageSize,pageNumber);
        //获取分页信息
        Page<DispatchVo> dispatchVoPage = dispatchMapper.page(page,name);
        //集合所有分页信息
        List<DispatchVo> dispatchVos =dispatchVoPage.getRecords();
        //遍历
        for (DispatchVo dispatchVo : dispatchVos) {
            //根据id放入对应子表信息
            Integer id = dispatchVo.getId();
            List<DispatchCityVo> cityVos = dispatchMapper.city(id);
            List<DispatchExpressVo> expresses = dispatchMapper.express(id);


            dispatchVo.setDispatchCityVos(cityVos);
            dispatchVo.setDispatchExpressVos(expresses);

        }
        return dispatchVoPage;
    }
}
